﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Net;
using Card.Security;
using System.IO;

namespace Card.Manager
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Handler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            if (context.Request.QueryString["t"] != null)
            {
                string t = context.Request.QueryString["t"];
                switch (t)
                {
                    case "ga":
                        GetAreaData(context);
                        break;
                    default:
                        break;
                }
            }
            else
            {
                string result = string.Empty;
                try
                {
                    string url = context.Request.QueryString["url"];
                    WebRequest request = HttpWebRequest.Create(url);
                    request.Method = "GET";
                    request.Headers.Add("Cookie", string.Format("{0}={1}", BAF.Security.CookieManager.PassportCookieName, CookieManager.GetCookie(BAF.Security.CookieManager.PassportCookieName)));
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                    using (StreamReader dataStream = new StreamReader(response.GetResponseStream()))
                    {
                        result = dataStream.ReadToEnd();
                    }
                }
                catch (Exception ex)
                {
                    result = string.Format("{{'r':'n','msg':'{0}'}}", ex.Message);
                }
                context.Response.ContentType = "text/plain";
                context.Response.Write(result);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private void GetAreaData(HttpContext context)
        {
            if (context.Request.QueryString["id"] != null && Utility.ValidationHelper.IsNumber(context.Request.QueryString["id"]))
            {
                int parentID = int.Parse(context.Request.QueryString["id"]);
                List<Entity.Area> lstArea = BLL.AreaManager.GetListByParentID(parentID, null);
                string strJson = string.Empty;
                if (lstArea != null && lstArea.Count > 0)
                {
                    strJson = string.Format("{{'{0}': {1} }}", parentID, Utility.JsonHelper.ObjectListToJson<Entity.Area>(lstArea));
                }
                else
                {
                    strJson = "{}";
                }
                context.Response.ContentType = "text/plain";
                context.Response.Write(strJson);                
            }
        }
    }
}
